using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;

namespace Projects.OssDownload.Pages
{
    public class IndexModel : PageModel
    {
        [BindProperty]
        [Required(ErrorMessage = "Remote URL is required.")]
        public string RemoteUrl { get; set; }

        [BindProperty]
        public string ProxyDownloadLink { get; set; }

        public void OnGet()
        {
            // Initialize any properties needed on GET request
        }

        public async Task<IActionResult> OnPostAsync()
        {
            await Task.CompletedTask;
            if (!ModelState.IsValid)
            {
                return Page();
            }

            // Construct the proxy download link
            var baseUrl = $"{Request.Scheme}://{Request.Host}/dl/";
            var encodedPath = System.Web.HttpUtility.UrlEncode(RemoteUrl.Replace("https://", ""));
            ProxyDownloadLink = $"{baseUrl}{encodedPath}";

            return Page();
        }
    }
}